<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>修改密码</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="../../layui/css/layui.css" rel="stylesheet">
</head>
<body>
<div class="layui-layout layui-layout-admin">
  <!---头部导航栏-->
  <div class="layui-header">
    <div class="layui-logo layui-hide-xs layui-bg-black"> <img src="//unpkg.com/outeres@0.0.10/img/layui/icon-v2.png" class="layui-nav-img">小药店管理平台</div>
    <!-- 头部区域（可配合layui 已有的水平导航） -->
    <ul class="layui-nav layui-layout-left">
      <!-- 移动端显示 -->
      <li class="layui-nav-item layui-show-xs-inline-block layui-hide-sm" lay-header-event="menuLeft">
        <i class="layui-icon layui-icon-spread-left"></i>
      </li>
      <li class="layui-nav-item layui-hide-xs"><a href="javascript:;">前台页面</a></li>
    </ul>
    <ul class="layui-nav layui-layout-right">
      <li class="layui-nav-item layui-hide layui-show-sm-inline-block">
        <a href="javascript:;">
          <img src="/Users/chenminggang/Downloads/20200829200211_enkci.jpg" class="layui-nav-img" >
          叮无缝
        </a>
        <dl class="layui-nav-child">
          <dd><a href="javascript:;">文件</a></dd>
          <dd><a href="javascript:;">设置</a></dd>
          <dd><a href="javascript:;">登出</a></dd>
        </dl>
      </li>
      <li class="layui-nav-item" lay-header-event="menuRight" lay-unselect>
        <a href="javascript:;">
          <i class="layui-icon layui-icon-more-vertical"></i>
        </a>
      </li>
    </ul>
  </div>

  <!---菜单导航栏-->
  <div class="layui-side layui-bg-black">
    <div class="layui-side-scroll">
      <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
      <ul class="layui-nav layui-nav-tree" lay-filter="test">
        <li class="layui-nav-item layui-nav-itemed">
          <a class="" href="javascript:;">采购管理</a>
          <dl class="layui-nav-child">
            <dd><a href="../purchaseManage/gongyingshangxinxi.html">供应商信息管理</a></dd>
            <dd><a href="../purchaseManage/yaopinxinxi.html">药品信息管理</a></dd>
            <dd><a href="../purchaseManage/caigoudan.html">采购单添加</a></dd>
            <dd><a href="../purchaseManage/PurchaseDetail.html">采购明细管理</a></dd>
          </dl>
        </li>
        <li class="layui-nav-item">
          <a href="javascript:;">销售管理</a>
          <dl class="layui-nav-child">
            <dd><a href="../salemanage/addsale.html">销售单添加管理</a></dd>
            <dd><a href="../salemanage/saledetailmanage.html">销售明细管理</a></dd>
          </dl>
        </li>
        <li class="layui-nav-item">
          <a href="javascript:;">药店统计管理</a>
          <dl class="layui-nav-child">
            <dd><a href="../medicalstatisticmanage/Inboundstatistic.html">入库统计</a></dd>
            <dd><a href="../medicalstatisticmanage/salestatistic.html">销售统计</a></dd>
            <dd><a href="../medicalstatisticmanage/inventoryinquiry.html">库存查询</a></dd>
          </dl>
        </li>

        <li class="layui-nav-item">
          <a href="javascript:;">人事管理</a>
          <dl class="layui-nav-child">
            <dd><a href="../peoplemanage/addpeople.html">员工添加</a></dd>
            <dd><a href="../peoplemanage/managepeople.html">员工管理</a></dd>
          </dl>
        </li>

        <li class="layui-nav-item">
          <a href="javascript:;">打印管理</a>
          <dl class="layui-nav-child">
            <dd><a href="../print/enterlibrary.html">入库单</a></dd>
            <dd><a href="../print/exitlibrary.html">出库单</a></dd>
          </dl>
        </li>

        <li class="layui-nav-item">
          <a href="javascript:;">系统管理</a>
          <dl class="layui-nav-child">
            <dd class="layui-this"><a href="javascript:;">修改密码</a></dd>
          </dl>
        </li>
      </ul>
    </div>
  </div>

  <!---内容-->
  <div class="layui-body">
    <!-- 内容主体区域 -->
    <div style="padding: 15px;">
      <form class="layui-form" id="update-password-form">
        <div class="demo-reg-container">
          <div class="layui-form-item">
            <div class="layui-input-wrap">
              <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-username"></i>
              </div>
              <input id="user_name" type="text" name="user_name" value="" lay-verify="required" placeholder="用户名" autocomplete="off" class="layui-input" id="username">
            </div>
          </div>
          <div class="layui-form-item">
            <div class="layui-input-wrap">
              <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-password"></i>
              </div>
              <input type="password" name="user_pwd" value="" lay-verify="required" placeholder="原密码" autocomplete="off" class="layui-input" id="user_pwd" lay-affix="eye">
            </div>
          </div>
          <div class="layui-form-item">
            <div class="layui-input-wrap">
              <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-password"></i>
              </div>
              <input type="password" name="newPassword" value="" lay-verify="required" placeholder="新密码" autocomplete="off" class="layui-input" id="new-password" lay-affix="eye">
            </div>
          </div>
          <div class="layui-form-item">
            <div class="layui-input-wrap">
              <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-password"></i>
              </div>
              <input type="password" name="confirmPassword" value="" lay-verify="required|confirmPassword" placeholder="确认密码" autocomplete="off" class="layui-input" lay-affix="eye">
            </div>
            <div><br/></div>
          <div class="layui-form-item">
            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-reg">修改</button>
          </div>
          </div>
        </div>
      </form>
    </div>
  </div>

  <!---底部导航栏-->
  <div class="layui-footer">
    <!-- 底部固定区域 -->
    底部固定区域
  </div>
</div>

<script src="../../layui/layui.js"></script>
<script>
  layui.use(function(){
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var util = layui.util;

    var sysAdminInfo = JSON.parse(localStorage.getItem("sysAdminInfo"));
    $('#user_name').val(sysAdminInfo.username);
    // 自定义验证规则
    form.verify({
      // 确认密码
      confirmPassword: function(value, item){
        var passwordValue = $('#new-password').val();
        if(value !== passwordValue){
          return '两次密码输入不一致';
        }
      }
    });
    var encryptedUserPwd = localStorage.getItem("encryptedUserPwd");
    $('#user_pwd').val(encryptedUserPwd);

    // 表单提交事件
    form.on('submit(demo-reg)', function(obj) {
      var formData = obj.field;
      $.ajax({
        url: "/api/updatePassword", // 后端接口
        type: "post", // 确保使用 POST 方法
        data: JSON.stringify(formData), // 提交参数
        contentType: "application/json;charset=utf-8",
        dataType: "json", // 后端返回的参数类型：json、html、text......
        success: function(res) { // 成功后的回调函数 res = RespResult
          if (res.code == 0) {
            layer.msg(res.message, {
              offset: '15px',
              icon: 1,
              time: 1000
            }, function() {
              location.href = '/login.html'; // 登录页面
            });
          } else {
            // 错误提示
            layer.msg(res.message, {
              offset: '15px',
              icon: 5,
              time: 2000
            });
          }
        },
        error: function(xhr, status, error) {
          console.error("AJAX 请求失败:", xhr, status, error);
          layer.msg("修改失败，请重试", {
            offset: '15px',
            icon: 5,
            time: 2000
          });
        }
      });
      return false; // 阻止表单默认提交行为
    });
  });
</script>
</body>
</html>